package lab15notepad;

public class NotepadMenu {
	private static final int MAIN_MENU_OPTION_COUNT = 3;
	private static final int USE_OPTION_COUNT = 9;

	public static void mainMenu() {
		System.out.println();
		System.out
				.println("Welcome to the main menu of NotePad--. Please choose what to do: ");
		System.out.println("1. Create a simple notepad.");
		System.out.println("2. Create a protected notepad.");
		System.out.println("3. Close the program and use a better text editor.");
		int choice = 0;
		while (choice <= 0 || choice > MAIN_MENU_OPTION_COUNT)
			choice = Input.getInt();
		switch (choice) {
		case 1:
			createSimple();
			break;
		case 2:
			createProtected();
			break;
		case 3:
			close();
			break;
		}
	}

	private static void close() {
		System.out.println("Thank you for using our product.");
	}

	private static void createSimple() {
		System.out.println();
		System.out.println("Enter the notepad title: ");
		String title = Input.getLine();
		System.out.println("Enter the number of pages: ");
		int pages = Input.getInt();
		INotepad notepad = new SimpleNotepad(title, pages);
		useNotepad(notepad);
	}

	private static void createProtected() {
		System.out.println();
		System.out.println("Enter the notepad title: ");
		String title = Input.getLine();
		System.out.println("Enter the number of pages: ");
		int pages = Input.getInt();
		System.out.println("Enter the notepad password: ");
		String password = Input.getLine();
		while (!SecureNotepad.passwordSecure(password)) {
			System.out.println("The password isn't secure. Your password must have a length of at least 5 and contain a lower case letter, an upper case letter and a digit.");
			password=Input.getLine();
		}
		INotepad notepad = new SecureNotepad(title, pages, password);
		useNotepad(notepad);
	}

	private static void useNotepad(INotepad notepad) {
		System.out.println();
		System.out.println("What do you want to do?");
		System.out.println("1. Show the contents of a page.");
		System.out.println("2. Show the contents of all pages.");
		System.out.println("3. Set the title of a page.");
		System.out.println("4. Add text to the end of a page.");
		System.out.println("5. Replace the text on a page.");
		System.out.println("6. Delete the text on a page.");
		System.out.println("7. Print all pages that contain digits.");
		System.out.println("8. Check if the notepad contains a certian word.");
		System.out.println("9. Return to main menu.");
		int choice = 0;
		while (choice <= 0 || choice > USE_OPTION_COUNT)
			choice = Input.getInt();
		switch (choice) {
		case 1:
			showPage(notepad);
			break;
		case 2:
			showAll(notepad);
			break;
		case 3:
			setTitle(notepad);
			break;
		case 4:
			addText(notepad);
			break;
		case 5:
			replaceText(notepad);
			break;
		case 6:
			deleteText(notepad);
			break;
		case 7:
			printAllPagesWithDigits(notepad);
			break;
		case 8:
			containsWord(notepad);
			break;
		case 9:
			leaveNotepad(notepad);
			break;
		}
	}

	private static void containsWord(INotepad notepad) {
		System.out.println("Enter the word you want to look for: ");
		String word = Input.getLine();
		notepad.searchWord(word);
		useNotepad(notepad);
	}

	private static void printAllPagesWithDigits(INotepad notepad) {
		notepad.printAllPagesWithDigits();
		useNotepad(notepad);
	}

	private static void leaveNotepad(INotepad notepad) {
		System.out.println("Closing notepad.");
		mainMenu();
	}

	private static void deleteText(INotepad notepad) {
		System.out.println("Enter the page you want to change: ");
		int page = Input.getInt();
		notepad.deleteText(page);
		useNotepad(notepad);
	}

	private static void replaceText(INotepad notepad) {
		System.out.println("Enter the page you want to change: ");
		int page = Input.getInt();
		System.out.println("Enter the text you want: ");
		String text = Input.getLine();
		notepad.replaceText(text, page);
		useNotepad(notepad);
	}

	private static void addText(INotepad notepad) {
		System.out.println("Enter the page you want to change: ");
		int page = Input.getInt();
		System.out.println("Enter the text you want: ");
		String text = Input.getLine();
		notepad.addText(text, page);
		useNotepad(notepad);
	}

	private static void setTitle(INotepad notepad) {
		System.out.println("Enter the page you want to change: ");
		int page = Input.getInt();
		System.out.println("Enter the title you want: ");
		String title = Input.getLine();
		notepad.setTitle(title, page);
		useNotepad(notepad);
	}

	private static void showAll(INotepad notepad) {
		notepad.showAll();
		useNotepad(notepad);
	}

	private static void showPage(INotepad notepad) {
		System.out
				.print("Please enter the number of the page you want to view: ");
		int page = Input.getInt();
		notepad.showPage(page);
		useNotepad(notepad);
	}
}
